/**
 * 
 */
package com.tdkj.action.front.member.account;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;

import com.tdkj.action.BaseAction;
import com.tdkj.model.enterprisezone.BeanEnterprise;
import com.tdkj.model.user.BeanUser;
import com.tdkj.model.user.BeanUserPersonal;
import com.tdkj.service.user.IBeanUserService;

public class AccountAction extends BaseAction {

	private BeanUser bean;
	private BeanEnterprise beanEnterprise;
	private IBeanUserService userService;

	public String execute() {
		bean = this.getCurrentUser();
		if (bean.getAccountType() == 1)
			beanEnterprise = bean.getEnterprise();
		return "success";
	}

	public String modify() {
		bean = this.getCurrentUser();
		beanEnterprise = bean.getEnterprise();
		return "modify";
	}

	public String update() {
		BeanUserPersonal beanUserPersonal = bean.getPersonal();
		if (StringUtils.isEmpty(beanUserPersonal.getImgUrl())) {
			BeanUserPersonal temp = this.getBaseService().findById(beanUserPersonal.getId(), BeanUserPersonal.class);
			if (null != temp)
				beanUserPersonal.setImgUrl(temp.getImgUrl());
		}
		userService.saveOrUpdate(beanEnterprise, BeanEnterprise.class);
		userService.saveOrUpdate(beanUserPersonal, BeanUserPersonal.class);
		return execute();
	}

	public String level() {
		bean = userService.findById(this.getCurrentUser().getId(), BeanUser.class);
		return "level";
	}

	public String passwordModify() {
		bean = userService.findById(this.getCurrentUser().getId(), BeanUser.class);
		return "passwordModify";
	}

	public String passwordUpdate() {
		String password = this.getQueryInfo().get("password");
		String newpassword = this.getQueryInfo().get("newpassword");
		String newpasswordRepeat = this.getQueryInfo().get("newpasswordRepeat");

		if (userService.isExist(this.getCurrentUser().getId(), password) && newpassword != null && newpassword.equals(newpasswordRepeat)) {
			userService.updatePassword(this.getCurrentUser().getId(), newpassword);
		}

		return passwordModify();
	}

	public BeanUser getBean() {
		return bean;
	}

	public void setBean(BeanUser bean) {
		this.bean = bean;
	}

	@Resource
	public void setUserService(IBeanUserService userService) {
		this.userService = userService;
	}

	/**
	 * @return the beanEnterprise
	 */
	public BeanEnterprise getBeanEnterprise() {
		return beanEnterprise;
	}

	/**
	 * @param beanEnterprise
	 *            the beanEnterprise to set
	 */
	public void setBeanEnterprise(BeanEnterprise beanEnterprise) {
		this.beanEnterprise = beanEnterprise;
	}

}
